package hotel;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Font;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.sql.SQLException;
import java.util.List;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class GestHotel extends JFrame implements ActionListener,KeyListener{
	ImageIcon iconax=new ImageIcon("Risorse/x4.png");
	private Salvataggio gestione=new Salvataggio();
	private Database d;
	private static JFrame finestrella;
	private JLabel testoNomeHotel;
	private JLabel testoNumCamere;
	private JTextField insNomeHotel;
	private JTextField insNumCamere;
	private JPanel pannelloFinestrella;
	private JButton ok,avanti;
	private JLabel benvenuto;
	private String welcome="Benvenuti! Questo software è un progetto ideato e realizzato completamente" +
			" da Mario Monaco ed Antonio Mungari.";
	private JFrame finestraBenvenuto;
	private JPanel panel;
	
	public GestHotel(){
			Image immagineIc = Toolkit.getDefaultToolkit().getImage("Risorse/icon.png");
		/*com.apple.eawt.Application application =com.apple.eawt.Application.getApplication();
		application.setDockIconImage(immagineIc);*/
		try {
			d=new Database();
		} catch (SQLException | IOException e1) {
			e1.printStackTrace();
		}
		if(new File("Risorse/fileSalvataggio.gtH").exists()){
			try {
				ObjectInputStream ois = new ObjectInputStream(new FileInputStream("Risorse/fileSalvataggio.gtH"));
				gestione.setNome((String) ois.readObject());
				gestione.setNumeroCamere((Integer) ois.readObject());
				ois.close();
				GraficaHotel g=new GraficaHotel(gestione,d);
				g.setVisible(true);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}else{
			finestraBenvenuto=new JFrame();
			finestraBenvenuto.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
			finestraBenvenuto.setTitle("Benvenuti!");
			finestraBenvenuto.setSize(300,300);
			finestraBenvenuto.setLocation(270,150);
			finestraBenvenuto.setLayout(new BorderLayout());
			finestraBenvenuto.setResizable(false);
			benvenuto=new JLabel(welcome);
			benvenuto.setForeground(Color.BLACK);
			benvenuto.setFont(Font.getFont(Font.SERIF));
			avanti=new JButton("Continua con l'installazione");
			avanti.addActionListener(this);
			panel=new JPanel(new BorderLayout());
			ImageIcon immagine=new ImageIcon("Risorse/benvenuti-lingue.jpg");
			JLabel sfondo=new JLabel(immagine);
			panel.setBackground(Color.WHITE);
			panel.add(sfondo,BorderLayout.NORTH);
			panel.add(benvenuto,BorderLayout.CENTER);
			panel.add(avanti,BorderLayout.SOUTH);
			finestraBenvenuto.add(panel);
			finestraBenvenuto.setVisible(true);
			finestraBenvenuto.pack();
		}
	}
	
	private void creaFinestrella() {
		finestrella = new JFrame("Crea nuova gestione");
		finestraBenvenuto.setFocusableWindowState(false);
		finestraBenvenuto.setEnabled(false);
		finestrella.setResizable(false);
		finestrella.setLayout(new BorderLayout());
		finestrella.setLocation(300, 200);
		finestrella.addWindowListener(new WindowAdapter() {
			public void windowClosing(WindowEvent e) {
				GestHotel.finestrella.dispose();
				GestHotel.this.finestraBenvenuto.setFocusableWindowState(true);
				GestHotel.this.finestraBenvenuto.setEnabled(true);
			}}
		);
		ok = new JButton("OK");
		ok.addActionListener(this);
		ok.addKeyListener(this);
		pannelloFinestrella = new JPanel();
		finestrella.add(pannelloFinestrella, BorderLayout.CENTER);
		testoNomeHotel = new JLabel("Nome hotel:");
		testoNumCamere = new JLabel("Numero camere:");
		insNomeHotel = new JTextField(10);
		insNomeHotel.addKeyListener(this);
		insNumCamere = new JTextField(10);
		insNumCamere.addKeyListener(this);
		pannelloFinestrella.add(testoNomeHotel);
		pannelloFinestrella.add(insNomeHotel);
		pannelloFinestrella.add(testoNumCamere);
		pannelloFinestrella.add(insNumCamere);
		pannelloFinestrella.add(ok);
		finestrella.setVisible(true);
		finestrella.setAlwaysOnTop(true);
		finestrella.pack();
	}
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		GestHotel i=new GestHotel();
	}
	@Override
	public void keyTyped(KeyEvent e) {
		// TODO Auto-generated method stub
		
	}
	@Override
	public void keyPressed(KeyEvent e) {
		if(e.getKeyCode()==KeyEvent.VK_ENTER){
			if (insNomeHotel.getText().compareTo("") == 0
					|| insNumCamere.getText().compareTo("") == 0 || 
					!insNumCamere.getText().matches("[0-9]*")){
				JOptionPane.showMessageDialog(null,
						"Devi completare entrambi i campi correttamente!","Errore",JOptionPane.ERROR_MESSAGE,iconax);
			}else{
				try {
					gestione.setNome(insNomeHotel.getText());
					gestione.setNumeroCamere(Integer.valueOf(insNumCamere.getText()));
					finestraBenvenuto.dispose();
					finestrella.dispose();
					SettaStanze s=new SettaStanze(gestione,d);
				} catch (Exception exc){}
			}
		}
	}
	@Override
	public void keyReleased(KeyEvent e) {
		// TODO Auto-generated method stub
		
	}
	@Override
	public void actionPerformed(ActionEvent e) {
		if(e.getSource()==ok){
			if (insNomeHotel.getText().compareTo("") == 0
					|| insNumCamere.getText().compareTo("") == 0 || 
					!insNumCamere.getText().matches("[0-9]*")){
				JOptionPane.showMessageDialog(null,
						"Devi completare entrambi i campi correttamente!","Errore",JOptionPane.ERROR_MESSAGE,iconax);
			}else{
					gestione.setNome(insNomeHotel.getText());
					gestione.setNumeroCamere(Integer.valueOf(insNumCamere.getText()));
					finestraBenvenuto.dispose();
					finestrella.dispose();
					SettaStanze s=new SettaStanze(gestione,d);
			}
		}else if(e.getSource()==avanti){
			creaFinestrella();
		}
	}

}
